<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="资产编号" prop="assetNumber">
        <el-input
          v-model="queryParams.assetNumber"
          placeholder="请输入资产编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产名称" prop="assetName">
        <el-input
          v-model="queryParams.assetName"
          placeholder="请输入资产名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="supplier">
        <el-input
          v-model="queryParams.supplier"
          placeholder="请输入供应商"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="复制张数" prop="copyCount">
        <el-input
          v-model="queryParams.copyCount"
          placeholder="请输入复制张数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当期折旧" prop="depreciateNow">
        <el-input
          v-model="queryParams.depreciateNow"
          placeholder="请输入录入当期折旧"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['info:info:add']"
        >新增</el-button>
      </el-col>
            <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['info:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['info:info:remove']"
        >删除</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['info:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed />
      <el-table-column label="序号" align="center" prop="id" width="80" fixed="left" />
      <el-table-column label="资产编号" align="center" prop="assetNumber" width="120" fixed="left" />
      <el-table-column label="资产名称" align="center" prop="assetName" />
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="资产类别" align="center" prop="type" />
      <el-table-column label="使用部门" align="center" prop="department" />
      <el-table-column label="使用人" align="center" prop="user" />
      <el-table-column label="开始使用日期" align="center" prop="startUseDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startUseDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="资产原值" align="center" prop="originalValue" />
      <el-table-column label="折旧方式" align="center" prop="depreciationMethod" />
      <el-table-column label="预计使用年限" align="center" prop="estimatedLife" />
      <el-table-column label="已使用/总期数" align="center" prop="usedYear" />
      <el-table-column label="每月折旧额" align="center" prop="monthlyDepreciation" />
      <el-table-column label="残值率(%)" align="center" prop="residualRate" />
      <el-table-column label="期初净值" align="center" prop="initialNetValue" />
      <el-table-column label="期末净值" align="center" prop="endingNetValue" />
      <el-table-column label="录入期间" align="center" prop="entryPeriod" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['info:info:edit']">修改</el-button>
          <el-button link type="primary" icon="Edit" @click="handleAssetChange(scope.row)" v-hasPermi="['info:info:edit']">资产变动</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['info:info:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资产基本信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="infoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="资产编号" prop="assetNumber">
          <el-input v-model="form.assetNumber" placeholder="请输入资产编号" />
        </el-form-item>
        <el-form-item label="资产名称" prop="assetName">
          <el-input v-model="form.assetName" placeholder="请输入资产名称" />
        </el-form-item>
        <el-form-item label="规格型号" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="资产类型/部门类型" prop="assetCategory">
          <el-input v-model="form.assetCategory" placeholder="资产类型/部门类型" />
        </el-form-item>
        <el-form-item label="购入日期" prop="purchaseDate">
          <el-date-picker clearable
            v-model="form.purchaseDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择购入日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="录入期间" prop="entryPeriod">
          <el-input v-model="form.entryPeriod" placeholder="请输入录入期间" />
        </el-form-item>
        <el-form-item label="开始使用日期" prop="startUseDate">
          <el-date-picker clearable
            v-model="form.startUseDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择开始使用日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="存放位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入存放位置" />
        </el-form-item>
        <el-form-item label="使用部门" prop="useDepartment">
          <el-input v-model="form.useDepartment" placeholder="请输入使用部门" />
        </el-form-item>
        <el-form-item label="使用人" prop="user">
          <el-input v-model="form.user" placeholder="请输入使用人" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplier">
          <el-input v-model="form.supplier" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="复制张数" prop="copyCount">
          <el-input v-model="form.copyCount" placeholder="请输入复制张数" />
        </el-form-item>
        <el-form-item label="录入当期折旧" prop="depreciateNow">
          <el-input v-model="form.depreciateNow" placeholder="请输入录入当期折旧" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 资产变动对话框 -->
    <AssetChange ref="assetChangeRef" :current-asset="selectedAsset" @success="handleAssetChangeSuccess" />
  </div>
</template>

<script setup name="Info">
import { listInfo, getInfo, delInfo, addInfo, updateInfo, changeAsset } from "@/api/info/info";
import AssetChange from './components/AssetChange.vue'

const { proxy } = getCurrentInstance();

const infoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    assetNumber: null,
    assetName: null,
    supplier: null,
    addType: null,
    copyCount: null,
    depreciateNow: null,
  },
  rules: {
    assetNumber: [
      { required: true, message: "资产编号不能为空", trigger: "blur" }
    ],
    assetName: [
      { required: true, message: "资产名称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

const assetChangeRef = ref()
const selectedAsset = ref({})

/** 查询资产基本信息列表 */
function getList() {
  loading.value = true;
  listInfo(queryParams.value).then(response => {
    infoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    assetNumber: null,
    assetName: null,
    specification: null,
    assetCategory: null,
    purchaseDate: null,
    entryPeriod: null,
    startUseDate: null,
    location: null,
    useDepartment: null,
    user: null,
    supplier: null,
    addType: null,
    copyCount: null,
    depreciateNow: null,
    remark: null
  };
  proxy.resetForm("infoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加资产基本信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改资产基本信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["infoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除资产基本信息编号为"' + _ids + '"的数据项？').then(function() {
    return delInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('info/info/export', {
    ...queryParams.value
  }, `info_${new Date().getTime()}.xlsx`)
}

/** 资产变动按钮操作 */
function handleAssetChange(row) {
  selectedAsset.value = row
  console.log(selectedAsset.value)
  assetChangeRef.value.show()
}

/** 资产变动成功回调 */
function handleAssetChangeSuccess(data) {
  // 调用资产变动API
  changeAsset(data).then(response => {
    if (response.data.code === 200) {
      proxy.$modal.msgSuccess("资产变动成功");
    } else {
      proxy.$modal.msgError(response.data.msg);
    }
    getList();
  });
}

getList();
</script>
